package com.example.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.auth.domain.DataNode;
import com.example.config.exception.GlobalExcetion;
import com.example.feign.NodeFeignService;
import com.example.ribbon.service.SysTableRibbonService;
import com.example.system.domain.SysTableScript;
import com.example.system.service.SysTableScriptService;
import com.example.system.vo.SysTableScriptVo;
import com.example.util.Results;
import com.example.util.StringSplitUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/tablescript")
public class SystemTableScriptController {

    @Autowired
    private SysTableScriptService sysTableScriptService;

    @Autowired
    private NodeFeignService nodeFeignService;

    @Autowired
    private SysTableRibbonService  sysTableRibbonService;

    @PostMapping("saveorupdate")
    public JSONObject saveOrUpdate(@Validated @RequestBody SysTableScriptVo vo) throws GlobalExcetion {
        SysTableScript sysTableScript=sysTableScriptService.saveOrUpdateSysTableScript(vo);
        return Results.success(sysTableScript);
    }

    @GetMapping("/getone")
    public SysTableScript getOne(@RequestParam("id") String id) throws GlobalExcetion {
        return sysTableScriptService.getById(id);
    }

    @GetMapping("/getall")
    public List<SysTableScript> getAll(@RequestParam("serverName")String serveName) throws GlobalExcetion {
        return sysTableScriptService.getByServeName(serveName);
    }

    @GetMapping("/pushnode")
    public JSONObject pushNode(){
        List<SysTableScript> sysTableScriptList=sysTableScriptService.list();
        for(SysTableScript sysTableScript:sysTableScriptList){
            List<String> serverNames= StringSplitUtil.splitToList(sysTableScript.getServerName());
            for(String serverName:serverNames){
                System.out.println("推送到服务："+serverName);
                sysTableRibbonService.pushSysTableToNode(serverName);
            }
        }
        return Results.success("推送成功");
    }
}
